package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.oss.internal.OSSUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.commons.aliyun.utils.AliyunOssUtils;
import com.tanhua.commons.utils.FileUtils;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.FollowUser;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.VideoVo;
import com.tanhua.dubbo.server.api.UserInfoApi;
import com.tanhua.dubbo.server.api.mongo.VideoApi;
import com.tanhua.server.interceptor.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoService {

    @Autowired
    protected FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Autowired
    private AliyunOssUtils ossUtils;

    @Reference
    private VideoApi videoApi;

    @Reference
    private UserInfoApi userInfoApi;


    public ResponseEntity saveVideo(MultipartFile picFile, MultipartFile videoFile) throws IOException {

        User user = UserThreadLocal.get();

        // 上传图片
        String filePath = FileUtils.getFilePath(picFile.getOriginalFilename());
        String imageUrl = ossUtils.upload(filePath, picFile.getInputStream());

        //上传视频
        StorePath storePath = storageClient.uploadFile(videoFile.getInputStream(),
                videoFile.getSize(),
                StringUtils.substringAfter(videoFile.getOriginalFilename(), "."),null);


        Video video = new Video();
        video.setUserId(user.getId());
        video.setPicUrl(imageUrl);
        video.setVideoUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());

        videoApi.save(video);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity queryVideoList(Integer page, Integer pageSize) {

        PageResult result = videoApi.findAll(page, pageSize);

        List<Video> videos = (List<Video>) result.getItems();

        List<VideoVo> videoVoList = new ArrayList<>();

        for (Video record : videos) {
            UserInfo info = userInfoApi.findByUserId(record.getUserId());
            VideoVo videoVo = new VideoVo();
            BeanUtils.copyProperties(info,videoVo);
            BeanUtils.copyProperties(record,videoVo);
            videoVo.setCover(record.getPicUrl());
            videoVo.setId(record.getId().toHexString());
            videoVo.setSignature("我就是我~"); //TODO 签名
            videoVo.setHasFocus(0); //TODO 是否关注
            videoVo.setHasLiked(0); //是否点赞
            videoVoList.add(videoVo);
        }

        result.setItems(videoVoList);

        return ResponseEntity.ok(result);
    }

    /**
     * 关注用户
     */
    public ResponseEntity followUser(Long followUserId) {
        FollowUser followUser = new FollowUser();
        followUser.setUserId(UserThreadLocal.getUserId());
        followUser.setFollowUserId(followUserId);
        videoApi.followUser(followUser);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注
     */
    public ResponseEntity disFollowUser(Long followUserId) {
        videoApi.disFollowUser(UserThreadLocal.getUserId(), followUserId);
        return ResponseEntity.ok(null);
    }
}
